package cn.hdr.train.hash;

/**
 *
 * 基于数组实现hash表，无hash冲突情况
 * @author: hdr
 * @date: 2020-07-07 15:22
 */
public class HashTable {

    private Integer[] table;

    private int size;

    public HashTable(int capacity){
        table = new Integer[capacity];
    }

    public Integer get(int key) {
        if(key < 0)
            return null;

        return table[key % table.length];
    }

    public void put(int key,int value){
        if(key < 0)
            return;

        table[key % table.length] = value;
        size++;
    }

    public void delete(int key){
        if(key < 0)
            return;

        table[key % table.length] = null;
        size--;
    }

    /**
     * 有限制的测试：
     */
    public static void main(String[] args) {
        HashTable table = new HashTable(64);
        table.put(1,55);
        table.put(32,12);
        table.put(0,9);
        System.out.println(table.get(1));
        table.delete(1);
        System.out.println(table.get(55));
        System.out.println(table.get(0));
        System.out.println(table.get(1));

        //加入同时put 0 和 64 呢？  这就是hash冲突
        table.put(64,123456);
        System.out.println(table.get(0));
        System.out.println(table.get(64));


    }
}